package br.unidocs.web.bean;

import java.io.IOException;

import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import br.unidocs.servico.RelatorioEJB;

public class RelatorioBackingBean {

	@EJB
	private RelatorioEJB relatorioEJB;

	public void downloadRelatorioArquivosPorUsuario() {
		byte[] relatorio = relatorioEJB.gerarRelatorioArquivosPorUsuario();

		try {
			HttpServletResponse response = (HttpServletResponse) FacesContext
					.getCurrentInstance().getExternalContext().getResponse();
			response.setContentType("application/force-download");
			response.addHeader("Content-Disposition", "attachment; filename=\""
					+ "Arquivos_x_Usuarios.pdf" + "\"");
			response.setContentLength(relatorio.length);

			ServletOutputStream sos = response.getOutputStream();
			sos.write(relatorio);
			sos.close();
			FacesContext.getCurrentInstance().responseComplete();
		} catch (IOException e) {
			throw new RuntimeException(
					"Erro ao disponibilizar relatório para download.", e);
		}
	}
}
